package com.ruicar.afs.jobadmin.core.conf;

import com.ruicar.afs.jobadmin.core.scheduler.AfsJobScheduler;
import com.ruicar.afs.jobadmin.mapper.*;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * The type JobAdminConfig
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.06.13 14:17:06
 * @since 1.0
 */
@Component
public class JobAdminConfig implements InitializingBean, DisposableBean {

    /**
     * Admin config
     * <p>Description: </p>
     */
    private static JobAdminConfig adminConfig = null;

    /**
     * Gets admin config *
     *
     * @return the admin config
     */
    public static JobAdminConfig getAdminConfig() {
        return adminConfig;
    }


    // ---------------------- AfsJobScheduler ----------------------

    /**
     * Afs job scheduler
     * <p>Description: </p>
     */
    private AfsJobScheduler afsJobScheduler;

    /**
     * After properties set *
     *
     * @throws Exception exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        adminConfig = this;

        afsJobScheduler = new AfsJobScheduler();
        afsJobScheduler.init();
    }

    /**
     * Destroy *
     *
     * @throws Exception exception
     */
    @Override
    public void destroy() throws Exception {
        afsJobScheduler.destroy();
    }


    // ---------------------- afsJobScheduler ----------------------

    /**
     * 18 n
     * <p>Description: </p>
     */
// conf
    @Value("${com.ruicar.afs.cloud.job.admin.i18n}")
    private String i18n;

    /**
     * Access token
     * <p>Description: </p>
     */
    @Value("${com.ruicar.afs.cloud.job.admin.accessToken}")
    private String accessToken;
    /**
     * Trigger pool fast max
     * <p>Description: </p>
     */
    @Value("${com.ruicar.afs.cloud.job.admin.triggerpool.fast.max}")
    private int triggerPoolFastMax;

    /**
     * Trigger pool slow max
     * <p>Description: </p>
     */
    @Value("${com.ruicar.afs.cloud.job.admin.triggerpool.slow.max}")
    private int triggerPoolSlowMax;

    /**
     * Logretentiondays
     * <p>Description: </p>
     */
    @Value("${com.ruicar.afs.cloud.job.admin.logretentiondays}")
    private int logretentiondays;

    // dao, service

    /**
     * Afs job log mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobLogMapper afsJobLogMapper;
    /**
     * Afs job info mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobInfoMapper afsJobInfoMapper;
    /**
     * Afs job registry mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobRegistryMapper afsJobRegistryMapper;
    /**
     * Afs job group mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobGroupMapper afsJobGroupMapper;
    /**
     * Afs job log report mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobLogReportMapper afsJobLogReportMapper;
    /**
     * Data source
     * <p>Description: </p>
     */
    @Resource
    private DataSource dataSource;


    /**
     * Gets i 18 n *
     *
     * @return the i 18 n
     */
    public String getI18n() {
        return i18n;
    }

    /**
     * Gets access token *
     *
     * @return the access token
     */
    public String getAccessToken() {
        return accessToken;
    }


    /**
     * Gets trigger pool fast max *
     *
     * @return the trigger pool fast max
     */
    public int getTriggerPoolFastMax() {
        if (triggerPoolFastMax < 200) {
            return 200;
        }
        return triggerPoolFastMax;
    }

    /**
     * Gets trigger pool slow max *
     *
     * @return the trigger pool slow max
     */
    public int getTriggerPoolSlowMax() {
        if (triggerPoolSlowMax < 100) {
            return 100;
        }
        return triggerPoolSlowMax;
    }

    /**
     * Gets logretentiondays *
     *
     * @return the logretentiondays
     */
    public int getLogretentiondays() {
        if (logretentiondays < 7) {
            return -1;  // Limit greater than or equal to 7, otherwise close
        }
        return logretentiondays;
    }

    /**
     * Gets afs job log mapper *
     *
     * @return the afs job log mapper
     */
    public AfsJobLogMapper getAfsJobLogMapper() {
        return afsJobLogMapper;
    }

    /**
     * Gets afs job info mapper *
     *
     * @return the afs job info mapper
     */
    public AfsJobInfoMapper getAfsJobInfoMapper() {
        return afsJobInfoMapper;
    }

    /**
     * Gets afs job registry mapper *
     *
     * @return the afs job registry mapper
     */
    public AfsJobRegistryMapper getAfsJobRegistryMapper() {
        return afsJobRegistryMapper;
    }

    /**
     * Gets afs job group mapper *
     *
     * @return the afs job group mapper
     */
    public AfsJobGroupMapper getAfsJobGroupMapper() {
        return afsJobGroupMapper;
    }

    /**
     * Gets afs job log report mapper *
     *
     * @return the afs job log report mapper
     */
    public AfsJobLogReportMapper getAfsJobLogReportMapper() {
        return afsJobLogReportMapper;
    }


    /**
     * Gets data source *
     *
     * @return the data source
     */
    public DataSource getDataSource() {
        return dataSource;
    }

}
